package com.oocl.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oocl.dao.OrdersDao;
import com.oocl.dao.base.BaseDao;
import com.oocl.entity.Orders;
import com.oocl.service.OrdersService;
import com.oocl.util.VoUtil;
import com.oocl.vo.OrdersVo;

@Service(value="ordersService")
public class OrdersServiceImpl implements OrdersService {
	
	@Resource(name="ordersDao")
	private OrdersDao ordersDao;
	@Resource(name="baseDao")
	private BaseDao baseDao;

	public List<OrdersVo> findAllOrdersByIcnum(String icnum) {
		return VoUtil.translateList(OrdersVo.class, ordersDao.getOrdersByIcnum(icnum));
	}

	public List<OrdersVo> findUnreceivedOrdersByIcnum(String icnum) {
		List<Orders> oList=new ArrayList<Orders>();
		for (Orders o:ordersDao.getOrdersByIcnum(icnum)) {
			if (o.getOstate().equals("0")) {          //state=0表示未接单
				oList.add(o);
			}
			
		}
		return VoUtil.translateList(OrdersVo.class, oList);
	}

	public List<OrdersVo> findRefusedOrdersByIcnum(String icnum) {
		List<Orders> oList=new ArrayList<Orders>();
		for (Orders o:ordersDao.getOrdersByIcnum(icnum)) {
			if (o.getOstate().equals("2")) {          //state=2表示拒接单
				oList.add(o);
			}
			
		}
		return VoUtil.translateList(OrdersVo.class, oList);
	}

	public List<OrdersVo> findreceivedOrdersByIcnum(String icnum) {
		List<Orders> oList=new ArrayList<Orders>();
		for (Orders o:ordersDao.getOrdersByIcnum(icnum)) {
			if (o.getOstate().equals("1")) {          //state=1表示已接单
				oList.add(o);
			}
			
		}
		return VoUtil.translateList(OrdersVo.class, oList);
	}

	public List<OrdersVo> findFinishedOrdersByIcnum(String icnum) {
		List<Orders> oList=new ArrayList<Orders>();
		for (Orders o:ordersDao.getOrdersByIcnum(icnum)) {
			if (o.getOstate().equals("4")) {          //state=4表示已完成定单
				oList.add(o);
			}
			
		}
		return VoUtil.translateList(OrdersVo.class, oList);
	}
	
	public List<OrdersVo> findDeliveringOrdersByIcnum(String icnum) {
		List<Orders> oList=new ArrayList<Orders>();
		for (Orders o:ordersDao.getOrdersByIcnum(icnum)) {
			if (o.getOstate().equals("3")) {          //state=3表示正在配送定单
				oList.add(o);
			}
			
		}
		return VoUtil.translateList(OrdersVo.class, oList);
	}


	@Transactional
	public List<OrdersVo> updateOrdersStatus(String oid,String status) {    //改变订单状态
		List<Orders> oList=ordersDao.findOrderByOid(oid);
		for (Orders o:oList) {
			o.setOstate(status);
			baseDao.update(o);
		}
		return VoUtil.translateList(OrdersVo.class, oList);
	}

	@Override
	public List<OrdersVo> findOrdersByOId(String oid) {
		return VoUtil.translateList(OrdersVo.class, ordersDao.findOrderByOid(oid));
	}

//	@Override
//	public List<OrdersVo> getByMerchantWithIcum(String icum) {
//		List<String> ordersoids = ordersDao.getOrderIdByIcum(icum);
//		List<OrdersVo> results = new ArrayList<OrdersVo>();
//		for(String oid : ordersoids){
//			Orders vo = ordersDao.findOrderByOid(oid).get(0);
//			
//		}
//		return null;
//	}





}
